console.log("TCP loaded");

var e = [ "room_online_info", "heart_beat" ], t = function() {
    wxapp.TCP.connectStatus = wxapp.TCP.CONNECT_STATUS_FAIL;
};

wxapp.TCP = {
    CONNECT_STATUS_OK: 1,
    CONNECT_STATUS_OPENING: 2,
    CONNECT_STATUS_CLOSING: 3,
    CONNECT_STATUS_FAIL: 0,
    connectStatus: 0,
    isTimerInited: !1,
    tickCount: 0,
    connect: function(n) {
        var a = this;
        wxapp.TCP.connectStatus != wxapp.TCP.CONNECT_STATUS_OK && wxapp.TCP.connectStatus != wxapp.TCP.CONNECT_STATUS_OPENING && (wxapp.TCP.connectStatus = wxapp.TCP.CONNECT_STATUS_OPENING, 
        wx.connectSocket({
            url: n
        }), wx.onSocketOpen(function(e) {
            wxapp.TCP.connectStatus = wxapp.TCP.CONNECT_STATUS_OK, wxapp.TCP.isTimerInited || (wxapp.TCP.isTimerInited = !0), 
            wxfish.Timer.setTimer(cc.director, a.timerSchedule, 1);
        }), wx.onSocketError(function(e) {
            t(), wxapp.NotificationCenter.trigger(wxapp.EventType.TCP_ERROR);
        }), wx.onSocketClose(function(e) {
            wxapp(null, "WebSocket 已关闭！"), t(), wxapp.NotificationCenter.trigger(wxapp.EventType.TCP_CLOSE);
        }), wx.onSocketMessage(function(t) {
            var n = a.decodeMessage(t.data);
            if (null != n && "0000" != n) {
                var c = "[Receive TCP Msg]: " + unescape(n.replace(/\\u/gi, "%u")), o = n.substr(0, n.length - 0);
                if (null != o && o.length > 0) {
                    var p = JSON.parse(o);
                    -1 == e.indexOf(p.cmd) && wxapp(null, c), ddz.msgCache.blocked ? ddz.msgCache.pushMsg(p) : a.triggerMsg(p);
                }
            }
        }));
    },
    triggerMsg: function(e) {
        wxapp.NotificationCenter.trigger(wxapp.EventType.TCP_RECEIVE, e);
    },
    decodeMessage: function(e) {
        if ("undefined" != typeof ArrayBuffer && e instanceof ArrayBuffer) {
            for (var t = new Uint8Array(e), n = "", a = 0, c = t.length; a < c; a++) n += String.fromCharCode(t[a]);
            return n;
        }
        for (var o = (e = hall.GlobalFuncs.base64decodeRaw(e)).slice(0, 4), a = 0, c = (e = e.slice(4)).length; a < c; a++) {
            var p = e[a];
            p ^= o[a % 4], e[a] = p;
        }
        return hall.GlobalFuncs._utf8_decode(e);
    },
    timerSchedule: function() {
        wxapp.TCP.tickCount = (wxapp.TCP.tickCount + 1) % 3, wxapp.TCP.tickCount, wxapp.TCP.reConnet();
    },
    reConnet: function() {
        wxapp.TCP.connectStatus == wxapp.TCP.CONNECT_STATUS_FAIL && (wxapp.NotificationCenter.trigger(wxapp.EventType.TCP_RECONNECT), 
        wxapp.TCP.connect(wxapp.SystemInfo.webSocketUrl));
    },
    sendMsg: function(n) {
        if (wxapp.TCP.connectStatus == wxapp.TCP.CONNECT_STATUS_OK) {
            var a = JSON.stringify(n);
            e.indexOf(n.cmd), wx.sendSocketMessage({
                data: a,
                success: function(e) {},
                fail: function(e) {
                    var n = arguments[0];
                    n && "sendSocketMessage:fail taskID not exist" === n.errMsg && (wx.closeSocket(), 
                    t());
                },
                complete: function(e) {}
            });
        }
    },
    close: function() {
        wxapp.TCP.connectStatus = wxapp.TCP.CONNECT_STATUS_CLOSING, wx.closeSocket(), t();
    }
};